package com.dm.cloud.core.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;

public class RequsetArrtibutesUtil {

    public static void put(String key,Object value){
        try {
            RequestContextHolder.getRequestAttributes().setAttribute(key,value,RequestAttributes.SCOPE_REQUEST);
//            HttpServletRequest request=ApplicationUtil.getRequest();
//            if(request!=null){
//                request.getSession().setAttribute(key,value);
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static <T> T get(String key){
        try {
            Object data = RequestContextHolder.getRequestAttributes().getAttribute(key, RequestAttributes.SCOPE_REQUEST);
            if(data!=null){
                return (T)data;
            }
//            HttpServletRequest request=ApplicationUtil.getRequest();
//            if(request!=null){
//                Object data = request.getSession().getAttribute(key);
//                if(data!=null){
//                    return (T)data;
//                }
//            }
        }catch (Exception e){
//            e.printStackTrace();
        }
        return null;
    }

}
